# -*- coding: UTF-8 -*-

import urllib.request as urlrequest
from lxml import etree

'''
基金基本概况: Fund Overview
基金基本概况网址：http://fundf10.eastmoney.com/jbgk_0062279.html
根据基金的代码获取基金的基本信息：
getfundbasicinfo - 简要数据
getfundbasictable - 基本概况数据


'''
class HTMLFundOverview(object):

    def __init__(self, code_fund):

        self.fbp_url = 'http://fundf10.eastmoney.com/jbgk_' + code_fund + '.html'
        self.fbp_html = urlrequest.urlopen(self.fbp_url).read()
        self.fbp_tree = etree.HTML(self.fbp_html)

        # tree = etree.parse(fileName)  # fileName 是一个本地的html文件
        # tree = etree.HTML(page_text)  # page_text 是网页获取的str类型字符串


    def getfundbasicinfo(self):

        dc_fundbasicinfo = dict()
        em_labels = self.fbp_tree.xpath('//div[@class="bs_gl"]/p/label')

        for em_label in em_labels:
            # 获取list中的首个元素，并去掉字符串首尾的空格
            key = (em_label.xpath('./text()'))[0].strip()
            if (em_labels.index(em_label) == 1) or (em_labels.index(em_label) == 3):
                value = (em_label.xpath('./a/text()'))[0]
            else:
                value = (em_label.xpath('./span/text()'))[0].strip()
                if em_labels.index(em_label) == 4:
                    value = (value.split(' '))[0]
            dc_fundbasicinfo[key] = value

        for k, v in dc_fundbasicinfo.items():
            print(k, v)

        return dc_fundbasicinfo

    # 资产规模
    def get_assetsize(self):
        assert_size = (self.fbp_tree.xpath('//*[@id="bodydiv"]/div[8]/div[3]/div[1]/div[2]/p/label[5]/span/text()'))[0].strip()
        # print(float(assert_size.split('亿')[0]))
        return float((assert_size.split('亿'))[0])

    def getfundbasictable(self):

        dc_fundbasictable = dict()

        table = self.fbp_tree.xpath('//table[@class="info w790"]')

        # 打印 table
        # t = etree.tostring(table[0], encoding="utf-8", pretty_print=True)
        # print('tbody =  ' + t.decode('utf-8'))

        # xpath: //table[@class="info w790"]/tbody/tr, 浏览器会对html文本进行一定的规范化, 去掉xpath中的tbody即可
        em_trs = table[0].xpath('./tr')
        ths = table[0].xpath('./th/text()')
        tds = table[0].xpath('./td')
        # tds[0].xpath('./span[2]/span/text()') 返回一个list类型的值
        dc_fundbasictable[ths[0]] = self.list2str(tds[0].xpath('./span[2]/span/text()'))
        dc_fundbasictable[ths[1]] = self.list2str(tds[1].xpath('./text()'))

        for em_tr in em_trs:
            em_ths = em_tr.xpath('./th/text()')
            # em_ths1 = em_tr.xpath('./th')
            em_tds = em_tr.xpath('./td')

            if em_trs.index(em_tr) == 3:
                dc_fundbasictable[em_ths[0]] = self.list2str(em_tds[0].xpath('./text()'))
                dc_fundbasictable[em_ths[1]] = self.list2str(em_tds[1].xpath('./a/text()'))
            elif em_trs.index(em_tr) == 4 or em_trs.index(em_tr) == 5:
                dc_fundbasictable[em_ths[0]] = self.list2str(em_tds[0].xpath('./a/text()'))
                dc_fundbasictable[em_ths[1]] = self.list2str(em_tds[1].xpath('./a/text()'))
            elif em_trs.index(em_tr) == 7:
                dc_fundbasictable[em_ths[0]] = self.list2str(em_tds[0].xpath('./text()'))
                dc_fundbasictable[em_ths[1]] = self.list2str(em_tds[1].xpath('./span[1]/text()'))
            else:
                dc_fundbasictable[em_ths[0]] = self.list2str(em_tds[0].xpath('./text()'))
                dc_fundbasictable[em_ths[1]] = self.list2str(em_tds[1].xpath('./text()'))

        for k, v in dc_fundbasictable.items():
            print(k, v)

        return dc_fundbasictable

    def list2str(self, ls):

        s = ""
        for l in ls:
            s = s + str(l)
        return s

if __name__ == '__main__':
    tt = HTMLFundOverview('006279')
    tt.getfundbasicinfo()
    tt.get_assetsize()
    tt.getfundbasictable()